<?php

namespace App\Services\My;


use App\Model\IncomeExpense;

class InviteService
{

    /**
     * Note - 个人中心-分享赚钱
     * DateTime 2024/8/13 17:31
     * User Admin
     * @param mixed $id
     */
    public function inviteList(mixed $uid, $params)
    {
        $key = $params['key'] ?? null;

        $page = $params['page'] ?? 1;
        $pageSize = $params['page_size'] ?? 20;
        $type = $params['type'] ?: 1;     // 1-收益明细 2-邀请用户 3-排行榜 4-收益明细-按天统计 5-收益明细-某天详情

        $incomeExpenseModel = new IncomeExpense();
        if ($type == 1) {
            $data = $incomeExpenseModel->getInviteList($uid, $page, $pageSize);
            foreach ($data as &$value) {
                $value['extend'] = $value['extend'] ? json_decode($value['extend'], true) : [];
            }
            $active_time = "2022.10.24 - 2022.11.24";
            // Tool::JSON(\CodeData::SUCCESS, array('result' => 1, 'msg' => $data, "active_time" => $active_time), '');
            return formatData(['result' => 1, 'msg' => $data, "active_time" => $active_time]);
        } elseif ($type == 2) {
            [$data, $ext] = $incomeExpenseModel->getInviteUserList($uid, $key);
            foreach ($data as &$v) {
                if (str_contains($v['head_image'], 'http')) {
                    $v['head_image'] .= '?x-oss-process=image/resize,h_100,m_lfit';
                }
            }
            $active_time = "2022.10.24 - 2022.11.24";
            // Tool::JSON(\CodeData::SUCCESS, ['result' => 1, 'msg' => $data, 'active_time' => $active_time, 'ext' => $ext], '');
            return formatData(['result' => 1, 'msg' => $data, "active_time" => $active_time, 'ext' => $ext]);
        } elseif ($type == 3) {
            $amount_type = 2; //($sex == 2) ? 2 : 1; 男女用户邀请好友都获得积分
            $nicknames = ['小暧昧', '骄傲如初', '我叫没朋友', '需要你的季节', '恰似一缕微光', '花开半夏', '雾里看花', '万花筒', '原来不是风'];
            $data = $incomeExpenseModel->getInviteRank($amount_type, $page, $pageSize);
            var_dump($data);
            foreach ($data as &$v) {
                if (str_contains($v['head_image'], 'http')) {
                    $v['head_image'] .= '?x-oss-process=image/resize,h_100,m_lfit';
                }
            }
            $count = min(count($nicknames), count($data));
            for ($i = 0; $i < $count; $i++) {
                $data[$i]['nickname'] = $nicknames[$i];
            }
            $active_time = "2022.10.24 - 2022.11.24";
            // Tool::JSON(\CodeData::SUCCESS, array('result' => 1, 'msg' => $data, "active_time" => $active_time), '');
            return formatData(['result' => 1, 'msg' => $data, "active_time" => $active_time]);
        } elseif ($type == 4) {
            [$data, $ext] = $incomeExpenseModel->getIncomeByDay($uid);
            // Tool::JSON(\CodeData::SUCCESS, ['result' => 1, 'msg' => $data, 'ext' => $ext], '');
            return formatData(['result' => 1, 'msg' => $data, 'ext' => $ext]);
        } else {
            $date = $params['date'] ?? date('Y-m-d');
            [$ext, $header, $data, $flag] = $incomeExpenseModel->getIncomeDetail($uid, $date);
            // Tool::JSON(\CodeData::SUCCESS, ['result' => 1, 'msg' => $data, 'ext' => $ext, 'header' => $header, 'flag' => $flag], '');
            return formatData(['result' => 1, 'msg' => $data, 'ext' => $ext, 'header' => $header, 'flag' => $flag]);
        }

    }
}